
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import vo.OpcionalVO;

public class OpcionalDAO {
    
    public static List<OpcionalVO> getOpcionais() {

        Connection con = ConnectionFactory.getConnection();

        List<OpcionalVO> lista = new ArrayList<>();

        try {

            String query = "SELECT * FROM Opcional";
            query += " order by nome";
            PreparedStatement stmt = con.prepareStatement(query);
            //stmt.setIn
            ResultSet res = stmt.executeQuery();

            while (res.next()) {
                String nome = res.getString(1);
                float tarifa = res.getFloat(2);
               
                
                lista.add(new OpcionalVO(nome,tarifa));
            }
        } catch (Exception e) {
        }
        return lista;
    }
        public static void alteraOpcional(String Nome,String Tarifa)
    {
        Connection con= ConnectionFactory.getConnection();
        try{
            String query= "Update Opcional set tarifa=? where nome= ?";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setString(1, Tarifa);
            stmt.setString(2, Nome);
            
            stmt.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static boolean OpcionalExiste(String Nome){
        Connection con= ConnectionFactory.getConnection();
        
        try{
            String query = "Select * from Opcional where nome= ?";
            PreparedStatement stmt=con.prepareStatement(query);
            String nomeNovo;
            stmt.setString(1,Nome);
            ResultSet res = stmt.executeQuery();
            while(res.next()){
            nomeNovo = res.getString(1);
            
            if(nomeNovo.equals(Nome))
                return true;
            }
            //return true;
        }catch(Exception e){
            return false;
        }
        return false;
    }
    public static void insertOpcional(OpcionalVO opcional) {
          Connection con = ConnectionFactory.getConnection();
        
        try {

            String query = "INSERT INTO Opcional (nome,tarifa) VALUES(?,?)";
            PreparedStatement stmt = con.prepareStatement(query);
            
            stmt.setString(1,opcional.getNome());
            stmt.setFloat(2,opcional.getTarifa());
            stmt.executeUpdate();

           
        } catch (Exception e) {
        }
    }
    
    public static void deleteOpcionalPeloNome(String nome)
    {
        Connection con = ConnectionFactory.getConnection();
        
        try{
            
            String query = "Delete from Opcional where nome='"+nome+"'";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.executeUpdate();
            
        }catch(Exception e){e.printStackTrace();}
    }

    public static List<OpcionalVO> getOpcionalPeloNome(String nome) {
        Connection con = ConnectionFactory.getConnection();
        
        List<OpcionalVO> lista =new ArrayList<>();
        
        try{
            String query = "SELECT * FROM Opcional WHERE nome = ?";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setString(1, nome);
            ResultSet res = stmt.executeQuery();

            while (res.next()) {
                String Nome= res.getString(1);
                Float tarifa = res.getFloat(2);

             lista.add(new OpcionalVO(Nome,tarifa));}
                
        }catch(Exception e){
           e.printStackTrace();
        }
        return lista;
    }
    
}
